# Contributor: Daniel Corbe <daniel@corbe.net>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=frr
pkgver=10.0
pkgrel=1
pkgdesc="Free Range Routing is a fork of Quagga"
pkgusers="frr"
pkggroups="frr frrvty"
url="https://frrouting.org/"
arch="all"
license="GPL-2.0-or-later"
depends="iproute2 bash"
makedepends="
	autoconf
	automake
	bison
	bsd-compat-headers
	c-ares-dev
	elfutils-dev
	flex
	json-c-dev
	libcap-dev
	libtool
	libyang-dev<2.2
	linux-headers
	net-snmp-dev
	protobuf-c-dev
	py3-sphinx
	python3-dev
	readline-dev
	rtrlib-dev
	texinfo
	"
checkdepends="py3-pytest"
install="$pkgname.pre-install"
subpackages="$pkgname-dbg
	$pkgname-doc::noarch
	$pkgname-openrc
	$pkgname-pythontools::noarch
	$pkgname-rpki
	$pkgname-snmp
	"
source="https://github.com/FRRouting/frr/archive/frr-$pkgver.tar.gz
	frr.initd
	allow-invalid-nlri-attributes.patch
	"
builddir="$srcdir/frr-frr-$pkgver"

_sysconfdir=/etc/frr

# secfixes:
#   8.2.1-r0:
#     - CVE-2022-26125
#     - CVE-2022-26126
#     - CVE-2022-26127
#     - CVE-2022-26128
#     - CVE-2022-26129

prepare() {
	default_prepare
	./bootstrap.sh
}

build() {
	export PYTHON=python3

	CFLAGS="$CFLAGS -flto=auto" \
	./configure \
		--prefix=/usr \
		--localstatedir=/run/frr \
		--sbindir=/usr/lib/frr \
		--sysconfdir=$_sysconfdir \
		--libdir=/usr/lib/frr \
		--with-moduledir=/usr/lib/frr/modules \
		LIBTOOLFLAGS="-rpath /usr/lib/frr" \
		--disable-dependency-tracking \
		--enable-rpki \
		--with-libpam \
		--enable-doc \
		--enable-doc-html \
		--enable-snmp \
		--enable-fpm \
		--disable-protobuf \
		--disable-zeromq \
		--enable-ospfapi \
		--enable-bgp-vnc \
		--enable-multipath=256 \
		--enable-user=${pkgusers%% *} \
		--enable-group=${pkggroups%% *} \
		--enable-vty-group=frrvty \
		--enable-configfile-mask=0640 \
		--enable-logfile-mask=0640
	make
}

check() {
	export PYTHON=python3
	make check
}

package() {
	make DESTDIR="$pkgdir" install

	# Remove development files, static libraries and headers.
	# They are internal and should not be shipped as a package.
	# Compare https://github.com/FRRouting/frr/blob/frr-7.0/debian/rules#L89
	find "$pkgdir" \( -name '*.la' -o -name '*.a' -o -name 'lib*.so' \) -exec rm -f {} +
	rm -rf "$pkgdir"/usr/include
	rm -f "$pkgdir"/usr/lib/frr/ssd

	# FRR currently installs two flavours of scripts for starting and stopping it.
	# Remove the deprecated one.
	rm -f "$pkgdir"/usr/lib/frr/frr
	rm -f "$pkgdir"/usr/lib/frr/frr-reload

	install -d "$pkgdir"$_sysconfdir
	install -Dm644 tools/etc/frr/vtysh.conf \
		tools/etc/frr/frr.conf \
		tools/etc/frr/daemons \
		tools/etc/frr/support_bundle_commands.conf \
		-t "$pkgdir"$_sysconfdir
	chown -R ${pkgusers%% *}:${pkggroups%% *} "$pkgdir"$_sysconfdir
	install -Dm644 tools/etc/iproute2/rt_protos.d/frr.conf \
		-t "$pkgdir"/etc/iproute2/rt_protos.d
	install -Dm755 "$srcdir"/frr.initd "$pkgdir"/etc/init.d/frr
}

pythontools() {
	pkgdesc="$pkgdesc (tools)"
	depends="python3 py3-ipaddr"

	amove usr/lib/frr/frr-reload.py
}

rpki() {
	pkgdesc="$pkgdesc (BGP RPKI support)"

	amove usr/lib/frr/modules/bgpd_rpki.so
}

snmp() {
	pkgdesc="$pkgdesc (net-snmp bindings)"

	amove usr/lib/frr/modules/*_snmp.so \
		usr/lib/frr/libfrrsnmp.*
}

sha512sums="
f02fe672d3ab7382752f83a8ae1e6198e6b906b4ad31063aad18b803c8756b40fa1f280c5dafe872dea505bcc882fbb8392802411f9fac0ab0bd45dac8b3ed28  frr-10.0.tar.gz
f86bdf8f6dd89ba69d03a69bbe645b3b752d3fbb38913c5155536b8043b0493901ab078b7acb6ac463d3473c6492900d8a695582f66610955246052b19c0d70c  frr.initd
5b2d735da13ea469c07b254d870fa67f36edb8ccf9eb7073f8858a830e5ec6356a6a9ebe304523ced475ad49beadf09666212eedb65b41edde2d12043ff4b900  allow-invalid-nlri-attributes.patch
"
